/**
 * 
 */
package com.zc.BankCreditManagement.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zc.BankCreditManagement.ZCUtil.CodeProtect;

/**
 * @author ZhangCheng
 * @time 2019年9月20日
 */
@WebServlet("/isLoginServlet")
public class isLoginServlet extends HttpServlet {

	/**
	 * 一个特定标识符的序列号
	 */
	private static final long serialVersionUID = -5026711800142503411L;

	public isLoginServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 1.获取cookie的值
		resp.getWriter().write(isLogined(req));

	}
	/**
	 * 	判断是否登陆过
	 * 	@return String -> null 未登录
	 * 		userid 加密后的userid
	 */
	private String isLogined(HttpServletRequest req) {
		String msg = "null";
		Cookie[] cookies = req.getCookies();
//		   for(Cookie c : cookies) {
//			   System.out.print(c.getName() +" ");
//			   System.out.print(c.getValue() + "\n");
//		   }
		if (cookies != null) {// cookies不为空
			for (int i = 0; i < cookies.length; i++) {
				if ("peoId".equals(cookies[i].getName())) {// peoId和请求值相同 代表已经登陆
					if (cookies[i].getValue().equals(req.getParameter("peoId"))) {
						msg = new CodeProtect().deCode(cookies[i].getValue());
					}
				}
			}
		}
		return msg;
	}

}
